<script setup>
import Edit from './components/Edit.vue'
import Add from './components/Add.vue'
import { ref, onMounted } from 'vue'
import axios from 'axios'

// TODO: 列表渲染
// 思路: 定义响应list -> 调用接口获取数据 -> 渲染数据
const list = ref([])
const getList = async () => {
  const res = await axios.get('/list')
  list.value = res.data
}
onMounted(() => getList())

// TODO: 删除功能
const onDelete = (id) => {
  axios.delete(`/del/${id}`)
  getList()
}

// TODO: 添加功能
const addRef = ref(null)
function onAdd() {
  addRef.value.open()
}
const add = () => {
  getList()
}

// TODO: 编辑功能
const editRef = ref(null)
function onEdit(row) {
  editRef.value.open(row);  
}
// 更新列表
const upList = () => {
  getList();
}
</script>

<template>
  <div class="app">
    <el-table :data="list">  
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name" width="150"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作" width="150">  
        <template #default="{row}">
          <el-button type="primary" @click="onAdd" link>添加</el-button>
          <el-button type="primary" @click="onEdit(row)" link>编辑</el-button>
          <el-button type="danger" @click=onDelete(row.id) link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <Edit ref="editRef" @update-list="upList" />
  <Add ref="addRef" @add-add="add"/>
</template>

<style scoped>
.app {
  width: 980px;
  margin: 100px auto 0;
}
</style>
